<?php

namespace App\Http\Controllers\Data;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class ServeController extends Controller
{
    public function ReadContact()
    {
        $myfile = fopen('./contact.txt', 'r') or die('Unable to open file!');
        $file = fread($myfile, filesize('./contact.txt'));
        $array = explode('|分割|', $file);
        $list = array();
        for ($i = 0; $i < sizeof($array); ++$i) {
            $t = explode(':', $array[$i]);
            $list[$t[0]] = $t[1];
        }

        return json_encode($list);
    }

    public function ReadInformation()
    {
        $myfile = fopen('./information.txt', 'r') or die('Unable to open file!');
        $file = fread($myfile, filesize('./information.txt'));
        $array = explode('|分隔|', $file);
        $list = array();
        for ($i = 0; $i < sizeof($array); ++$i) {
            $t = explode(':', $array[$i]);
            $list[$t[0]] = $t[1];
        }
        return json_encode($list);
    }

    public function UpdateContact(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|',
            'PostalCode' => 'required',
            'fax' => 'required',
            'SalesTel' => 'required',
            'InterFax' => 'required',
            'Email1' => 'required',
            'Address' => 'required',
            'BusRoute' => 'required',
            'DepartmentTel' => 'required',
            'Email2' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $name = 'name:' . $request->name . '|分割|';
        $PostalCode = 'PostalCode:' . $request->PostalCode . '|分割|';
        $fax = 'fax:' . $request->fax . '|分割|';
        $SalesTel = 'SalesTel:' . $request->SalesTel . '|分割|';
        $InterFax = 'InterFax:' . $request->InterFax . '|分割|';
        $Email1 = 'E-mail1:' . $request->Email1 . '|分割|';
        $Address = 'Address:' . $request->Address . '|分割|';
        $BusRoute = 'BusRoute:' . $request->BusRoute . '|分割|';
        $DepartmentTel = 'DepartmentTel:' . $request->DepartmentTel . '|分割|';
        $Email2 = 'Email2:' . $request->Email2 . '|分割|';
        $messg = $name . $PostalCode . $fax . $SalesTel . $InterFax . $Email1 . $Address . $BusRoute . $DepartmentTel . $Email2;

        file_put_contents('./contact.txt', $messg);
        session()->flash('true', '修改成功!');
        $data = ['success' => true, 'message' => '修改成功'];
        return json_encode($data);
    }

    public function UpdateInformation(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'profile' => 'required',
            'history' => 'required',
            'u_id'=>'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $totalTDCode = glob('./temp/'.$request->u_id.'/TDCode' . '*');
        $TDCodeNumber = count($totalTDCode);

        $myfile = fopen('./information.txt', 'r') or die('Unable to open file!');
        $file = fread($myfile, filesize('./information.txt'));
        $array = explode('|分隔|', $file);
        $list = array();
        for ($i = 0; $i < sizeof($array); ++$i) {
            $t = explode(':', $array[$i]);
            $list[$t[0]] = $t[1];
        }
        $TDCode = $list['TDCode'];
        if ($TDCodeNumber == 0) {
            $TDCode = $list['TDCode'];
        } else {
            $TDCodehandler = opendir('./temp/'.$request->u_id.'/TDCode');
            while (($TDCodename = readdir($TDCodehandler)) !== false) {
                if ($TDCodename != '.' && $TDCodename != '..') {
                    $temp = explode('.', $TDCodename);
                    $extension = end($temp);
                    $picName = 'TDCode' . '.' . $extension;
                    $TDCode = './TDCode/' . $picName;
                    if (!file_exists($TDCode)) {
                        $counter_file = $TDCode;

                        $fopen = fopen($counter_file, 'wb '); //新建文件命令
                        fclose($fopen);
                    }
                    copy('./temp/'.$request->u_id.'/TDCode/' . 'TDCode' . '.' . $extension, $TDCode);
                }
            }
        }

        $profile = 'profile:' . $request->profile . '|分割|';
        $history = 'history:' . $request->history . '|分割|';
        $messg = $profile . $history . $TDCode;
        file_put_contents('./imformation.txt', $messg);
        $data = ['success' => true, 'message' => '修改成功'];
        
        if (file_exists('./temp/'.$request->u_id.'/TDCode')) {
            $dh = opendir('./temp/'.$request->u_id.'/TDCode');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/TDCode' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        return json_encode($data);
    }

    public function TmpClear(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'u_id'=>'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        if (file_exists('./temp/'.$request->u_id.'/product/CoverImg')) {
            $dh = opendir('./temp/'.$request->u_id.'/product/CoverImg');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/product/CoverImg' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        // if (file_exists('./temp/'.$request->u_id.'/product/ConModeImg')) {
        //     $dh = opendir('./temp/'.$request->u_id.'/product/ConModeImg');
        //     while ($file = readdir($dh)) {
        //         if ($file != "." && $file != "..") {
        //             $fullpath = './temp/'.$request->u_id.'/product/ConModeImg' . "/" . $file;
        //             if (!is_dir($fullpath)) {
        //                 unlink($fullpath);
        //             } else {
        //                 deldir($fullpath);
        //             }
        //         }
        //     }
        //     closedir($dh);
        // }
        // if (file_exists('./temp/'.$request->u_id.'/product/DetailsImg')) {
        //     $dh = opendir('./temp/'.$request->u_id.'/product/DetailsImg');
        //     while ($file = readdir($dh)) {
        //         if ($file != "." && $file != "..") {
        //             $fullpath = './temp/'.$request->u_id.'/product/DetailsImg' . "/" . $file;
        //             if (!is_dir($fullpath)) {
        //                 unlink($fullpath);
        //             } else {
        //                 deldir($fullpath);
        //             }
        //         }
        //     }
        //     closedir($dh);
        // }

        if (file_exists('./temp/'.$request->u_id.'/product/imgs')) {
            $dh = opendir('./temp/'.$request->u_id.'/product/imgs');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/product/imgs' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/product/Videos')) {
            $dh = opendir('./temp/'.$request->u_id.'/product/Videos');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/product/Videos' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/news/CoverImg')) {
            $dh = opendir('./temp/'.$request->u_id.'/news/CoverImg');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/news/CoverImg' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/news/ContentImg')) {
            $dh = opendir('./temp/'.$request->u_id.'/news/ContentImg');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/news/ContentImg' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/news/imgs')) {
            $dh = opendir('./temp/'.$request->u_id.'/news/imgs');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/news/imgs' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/news/Videos')) {
            $dh = opendir('./temp/'.$request->u_id.'/news/Videos');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/news/Videos' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/honour/CoverImg')) {
            $dh = opendir('./temp/'.$request->u_id.'/honour/CoverImg');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/honour/CoverImg' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
        if (file_exists('./temp/'.$request->u_id.'/TDCode')) {
            $dh = opendir('./temp/'.$request->u_id.'/TDCode');
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = './temp/'.$request->u_id.'/TDCode' . "/" . $file;
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
            closedir($dh);
        }
    }
}
